namespace Core.Base
{
    using System;
    using Contracts.Enums;
    using Contracts.Interfaces;
    using System.Linq;
    using System.Collections.Generic;
    using System.ComponentModel;

    /// <summary>
    /// Extension base class
    /// </summary>
    public abstract class ExtensionBase : PropertyChangedBase, IExtension, IEquatable<ExtensionBase>, IEquatable<IExtension>
    {
        private string _name;
        private ExtensionGroupsEnum _group;
        private ushort _complexity;
        private ushort _level;
        private IExtensionCost _installationCost;
        private IEnumerable<ICondition<IExtension>> _conditions;
        private IEnumerable<IModifier> _mods;
        /// <summary>
        /// Create a new instance of the <see cref="T:Core.Base.ExtensionBase"/> class.
        /// </summary>
        protected ExtensionBase(){ }
        /// <summary>
        /// Create a new instance of the <see cref="T:Core.Base.ExtensionBase"/> class.
        /// </summary>
        protected internal ExtensionBase(string name, ExtensionGroupsEnum group, ushort complexity, ushort level, IExtensionCost cost, IEnumerable<ICondition<IExtension>> conditions = null)
        {
            if(string.IsNullOrEmpty(name))throw new ArgumentNullException("name");
            if(group == ExtensionGroupsEnum.NotDefined) throw new InvalidEnumArgumentException("group");
            if(complexity < 1)throw new ArgumentOutOfRangeException("complexity");
            if(cost ==null)throw new ArgumentNullException("cost");
            if(level > 10)throw new ArgumentOutOfRangeException("level");

            _name = name;
            _group = group;
            _complexity = complexity;
            _level = level;
            _installationCost = cost;
            _conditions = conditions;
        }

        public IEnumerable<IModifier> Modifiers
        {
            get { return _mods; }
            set { _mods = value; }
        }
        /// <summary>
        /// Determines if there is any modifier with IsVisible set to true.
        /// </summary>
        public bool HasVisibleModifiers
        {
            get { return Modifiers != null && Modifiers.Where(m => m.IsVisible).Any(); }
        }
        /// <summary>
        /// Obtains or defines the extension name
        /// </summary>
        public string Name
        {
            get { return _name; }
            set
            {
                if (string.IsNullOrEmpty(value)) throw new ArgumentNullException("Name");
                _name = value;
            }
        }
        /// <summary>
        /// Obtains or defines the extension group
        /// </summary>
        public ExtensionGroupsEnum Group
        {
            get { return _group; }
            set
            {
                if (value == ExtensionGroupsEnum.NotDefined) throw new InvalidEnumArgumentException("Group");
                _group = value;
            }
        }
        /// <summary>
        /// Obtains or defines the extension complexity
        /// </summary>
        public ushort Complexity
        {
            get { return _complexity; }
            set
            {
                if (value < 1) throw new ArgumentOutOfRangeException("Complexity");
                _complexity = value;
            }
        }
        /// <summary>
        /// Obtains or defines the extension current level
        /// </summary>
        public ushort Level
        {
            get { return _level; }
            set
            {
                if (value > 10) throw new ArgumentOutOfRangeException("Level");
                _level = value; NotifyPropertyChange(() => Level);
            }
        }
       
        /// <summary>
        /// Obtains or defines the extension cost
        /// </summary>
        public IExtensionCost InstallationCost
        {
            get { return _installationCost; }
            set
            {
                if (value == null) throw new ArgumentNullException("InstallationCost");
                _installationCost = value;
            }
        }
        /// <summary>
        /// Determines if the extension has conditions
        /// </summary>
        public bool HasConditions
        {
            get { return Conditions!=null && Conditions.Any(); }
        }
        /// <summary>
        /// Obtains or defines the extension conditions
        /// </summary>
        public IEnumerable<ICondition<IExtension>> Conditions
        {
            get { return _conditions; }
            set { _conditions = value; }
        }
        /// <summary>
        /// Obtains the extension description
        /// </summary>
        public virtual string Description
        {
            get { return null; }
        }
        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        /// <param name="other">An object to compare with this object.</param>
        public bool Equals(ExtensionBase other)
        {
            if (ReferenceEquals(null, other)) return false;
            return ReferenceEquals(this, other) || Equals(other._name, _name);
        }
        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>. </param><filterpriority>2</filterpriority>
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            return obj is ExtensionBase && Equals(obj as ExtensionBase) ;
        }
        /// <summary>
        /// Serves as a hash function for a particular type. 
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public override int GetHashCode()
        {
            return base.GetHashCode() ;
        }
        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        /// <param name="other">An object to compare with this object.</param>
        public bool Equals(IExtension other)
        {
            if (ReferenceEquals(null, other)) return false;
            return ReferenceEquals(this, other) || Equals(other.Name, _name);
        }
        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public override string ToString()
        {
            return _name;
        }
    }
}